package zuoye5;

public class Site implements Runnable{
    private int count=100;
    private int num=0;
    boolean flag=false;

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            sale();
        }
    }

    public synchronized void sale(){
        if (count <= 0) {
            flag=true;
            return;
        }
        count--;
        num++;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票,剩余"+count+"张票");
    }

    public static void main(String[] args) {
        Site site=new Site();
        Thread t1=new Thread(site,"窗口1");
        Thread t2=new Thread(site,"窗口2");
        Thread t3=new Thread(site,"窗口3");
        Thread t4=new Thread(site,"窗口4");
        Thread t5=new Thread(site,"窗口5");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }
}
